﻿
using System;
using System.Collections.Generic;

namespace DrawTest
{
	
	public class AirCraftGenerator
	{
		public AirCraftGenerator()
		{
		}
		
		public void generatorAirCraft(int numberAirCraft,List <AirCraft> airCrafts,int allTime, Boolean military)
		{
			int seed = System.DateTime.Now.Millisecond + System.DateTime.Now.Second + System.DateTime.Now.Minute;
			Random rnd = new Random(seed);
			List<int> times = new List<int>();
			times = findTime(allTime, numberAirCraft);
			
			for (int i=0;i<numberAirCraft;i++)
			{
				int beginAngle = 0;
				int endAngle = 0;
				
				AirCraft airCraft = new AirCraft();
				int minAngle = 0;
				int maxAngle = 360;
				MyPoint coordCenterOfRotation = new MyPoint(Consts.CoordCenterOfRotationX,Consts.CoordCenterOfRotationY);
				
				
				beginAngle = rnd.Next(361);
				minAngle = beginAngle - 60;
				maxAngle = beginAngle + 60;
				Boolean greenLight = true;
				
				if (minAngle <= 0){
					greenLight = false;
					minAngle = 360 + minAngle;
					endAngle = rnd.Next(maxAngle, minAngle);
				}
				
				if (maxAngle > 360){
					greenLight = false;
					maxAngle = maxAngle - 360;
					endAngle = rnd.Next(maxAngle, minAngle);
				}
				
				if (greenLight){
					int newRandomNumber = rnd.Next(240);
					if (newRandomNumber <= minAngle){
						endAngle = rnd.Next(minAngle);
					} else {
						endAngle = rnd.Next(maxAngle, 361);
					}
					
				}
				
				MyAngleLen firstAnLen = new MyAngleLen(beginAngle,Consts.FarLineLenght-6);
				MyPoint startPoint = FlyingObject.FindDecartPoint(coordCenterOfRotation, firstAnLen);
				
				MyAngleLen secondAnLen = new MyAngleLen(endAngle,Consts.FarLineLenght-6);
				MyPoint endPoint = FlyingObject.FindDecartPoint(coordCenterOfRotation, secondAnLen);
				
				string picture;
				if (military){
					picture = "resources\\fly_small.png";
				} else{
					picture = "resources\\fly_circle.png";
				}
				
				airCraft.initAirCraft(picture,15,startPoint,endPoint,times[i]);
				airCraft.Civil = !military;
				airCrafts.Add(airCraft);	
			}
			
		}
		
		protected List<int> findTime(int allTime, int numberAirCraft)
		{
			int seed = System.DateTime.Now.Millisecond + System.DateTime.Now.Second + System.DateTime.Now.Minute;
			List<int> result = new List<int>();
			Random rnd = new Random(seed);
			
			List<int> times = new List<int>();
			
			int timeForBattle = (int)Math.Round((double)allTime/(double)numberAirCraft);
			if (timeForBattle < 1){
				timeForBattle = 1;
			}
			
			int time = 0;
			result.Add(time);
			
			for (int i=1;i<numberAirCraft;i++){
				
				time = time + timeForBattle;
				
				int ranTime = (int)Math.Round((double)rnd.Next(timeForBattle*10)/10);
				int sign = rnd.Next((timeForBattle*2 + 1)*100 );
				int resultTime = time;
				
				if (sign <= timeForBattle*100){
					resultTime = time + ranTime;
				} else if (sign >= timeForBattle*100 && sign <= timeForBattle*200){
					resultTime = time - ranTime;
				}
				result.Add(resultTime);
			}
			return result;
			
		}
		
	}
	
}

